<template>
  <div class="panel-tab__content">
    <div class="panel-tab__content--title">
      <span><t-icon name="menu" style="margin-right: 8px; color: #555" />消息列表</span>
      <t-button theme="primary" preIcon="ep:plus" @click="openModel('message')" size="small">创建新消息</t-button>
    </div>
    <t-table :data="messageList" bordered size="small" stripe :columns="messageColumns" row-key="index">
      <template #index="{ rowIndex }">  
        {{ rowIndex + 1 }}
      </template>
            
    </t-table>
    <div
      class="panel-tab__content--title"
      style="padding-top: 8px; margin-top: 8px; border-top: 1px solid #eee"
    >
      <span><t-icon name="menu" style="margin-right: 8px; color: #555" />信号列表</span>
      <t-button theme="primary"  preIcon="ep:plus" @click="openModel('signal')" size="small">创建新信号</t-button>
    </div>
    <t-table :data="signalList" bordered size="small" stripe :columns="signalColumns" row-key="index">
      <template #index="{ rowIndex }">  
        {{ rowIndex + 1 }}
      </template>
    </t-table>

    <t-dialog
      v-model:visible="dialogVisible"
      :header="modelConfig.title"
      :close-on-click-modal="false"
      width="400px"
      append-to-body
      destroy-on-close
    >
      <t-form :model="modelObjectForm" label-width="90px">
        <t-form-item :label="modelConfig.idLabel">
          <t-input v-model="modelObjectForm.id" clearable />
        </t-form-item>
        <t-form-item :label="modelConfig.nameLabel">
          <t-input v-model="modelObjectForm.name" clearable />
        </t-form-item>
      </t-form>
      <template #footer>
        <t-button @click="dialogVisible = false">取 消</t-button>
        <t-button theme="primary" @click="addNewObject">保 存</t-button>
      </template>
    </t-dialog>
  </div>
</template>
<script lang="ts" setup>
import { MessagePlugin, TableCol } from 'tdesign-vue-next'
import { computed, onMounted, ref } from 'vue'

defineOptions({ name: 'SignalAndMassage' })

const signalList = ref<any[]>([])
const messageList = ref<any[]>([])
const dialogVisible = ref(false)
const modelType = ref('')
const modelObjectForm = ref<any>({})
const rootElements = ref()
const messageIdMap = ref()
const signalIdMap = ref()
const modelConfig = computed(() => {
  if (modelType.value === 'message') {
    return { title: '创建消息', idLabel: '消息ID', nameLabel: '消息名称' }
  } else {
    return { title: '创建信号', idLabel: '信号ID', nameLabel: '信号名称' }
  }
})
const bpmnInstances = () => (window as any)?.bpmnInstances

const initDataList = () => {
  console.log(window, 'window')
  rootElements.value = bpmnInstances().modeler.getDefinitions().rootElements
  messageIdMap.value = {}
  signalIdMap.value = {}
  messageList.value = []
  signalList.value = []
  rootElements.value.forEach((el:any) => {
    if (el.$type === 'bpmn:Message') {
      messageIdMap.value[el.id] = true
      messageList.value.push({ ...el })
    }
    if (el.$type === 'bpmn:Signal') {
      signalIdMap.value[el.id] = true
      signalList.value.push({ ...el })
    }
  })
}
const openModel = (type:any) => {
  modelType.value = type
  modelObjectForm.value = {}
  dialogVisible.value = true
}
const addNewObject = () => {
  if (modelType.value === 'message') {
    if (messageIdMap.value[modelObjectForm.value.id]) {
      MessagePlugin.error('该消息已存在，请修改id后重新保存')
    }
    const messageRef = bpmnInstances().moddle.create('bpmn:Message', modelObjectForm.value)
    rootElements.value.push(messageRef)
  } else {
    if (signalIdMap.value[modelObjectForm.value.id]) {
      MessagePlugin.error('该信号已存在，请修改id后重新保存')
    }
    const signalRef = bpmnInstances().moddle.create('bpmn:Signal', modelObjectForm.value)
    rootElements.value.push(signalRef)
  }
  dialogVisible.value = false
  initDataList()
}
const messageColumns: TableCol[] = [
  { colKey: 'index', title: '序号', width:60},
  { colKey: 'id', title: '消息ID',ellipsisTitle: true, },
  { colKey: 'name', title: '消息名称',ellipsisTitle: true, },
].map(column => ({ ...column, align: 'center' }));
const signalColumns: TableCol[] = [
  { colKey: 'index', title: '序号', width:60},
  { colKey: 'id', title: '信号ID',ellipsisTitle: true, },
  { colKey: 'name', title: '信号名称',ellipsisTitle: true, },
].map(column => ({ ...column, align: 'center' }));
onMounted(() => {
  initDataList()
})
</script>
